package com.ebt.m.utils;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.net.json.CustomerDynamicJson;


/**
 * Created by andy on 16-5-24.
 */
public class CustomerActionUtil {

    public static final int CATEGORY_4CUSTOMER = 1;     //客户
    public static final int CATEGORY_4ATTACHMENT = 2;   //去掉
    public static final int CATEGORY_4NOTES = 3;        //笔记
    public static final int CATEGORY_4MAKEPROPOSAL = 4; //制作计划书
    public static final int CATEGORY_4SENDPROPOSAL = 5; //
    public static final int CATEGORY_4RESPONSEPROPOSAL = 6;
    public static final int CATEGORY_4BULKIMPORT = 7;
    public static final int CATEGORY_4RESPONSEMESSAGE = 8;
    public static final int CATEGORY_4CUSTOMERIMPORT = 9;

    public static final int VIEW_4CUSTOMER = 1;
    public static final int VIEW_4ATTACHMENT = 2;
    public static final int VIEW_4NOTES = 3;
    public static final int VIEW_4PROPOSAL = 4;
    public static final int VIEW_4TRACKVIEW = 5;



    public static SpannableString getActionStrForNet(Context context, String res, int activeType, String id, Object extra, boolean clickable) {
        SpannableString spannableString = new SpannableString(res);
        if (clickable) {
            spannableString.setSpan(new ClickSpanForNet(context, activeType, id, extra), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            spannableString.setSpan(
                    new ForegroundColorSpan(AppColors.getPrimary(context)),
                    0,
                    spannableString.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }

    public static String getFileName(String attachmentDes) {
        try {
            if (TextUtils.isEmpty(attachmentDes)) {
                return null;
            } else {
                return attachmentDes.split(CustomerConstant.CUSTOMER_FILE_DIVIDER)[1];
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String getFilePath(String attachmentDes) {
        try {
            if (TextUtils.isEmpty(attachmentDes)) {
                return null;
            } else {
                return attachmentDes.split(CustomerConstant.CUSTOMER_FILE_DIVIDER)[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void setTextWithSpanForDyJson2(Context context, CustomerDynamicJson json, int activeType, TextView textView) {
        if (textView == null) {
            throw new RuntimeException("TextView should not be null");
        }

        textView.setHighlightColor(context.getResources().getColor(R.color.home_click_bg_pressed));
        textView.setText("");
        switch (activeType) {
            case CustomerConstant.InteractionType.CUSTOMER_CREATE:
                textView.append("新建此客户");
                break;
            case CustomerConstant.InteractionType.INSURANCE_CREATE:
                textView.append("创建保单");
                if (json != null && json.objectFromActiveJson != null && json.objectFromActiveJson.policyName != null) {
                    textView.append(getActionStrForNet(context,
                            json.objectFromActiveJson.policyName,
                            activeType,
                            json.objectFromActiveJson.policyId,
                            null,
                            false));
                }
                break;
            case CustomerConstant.InteractionType.INSURANCE_EDIT:
                textView.append("编辑保单");
                if (json != null && json.objectFromActiveJson != null && json.objectFromActiveJson.policyName != null) {
                    textView.append(getActionStrForNet(context,
                            json.objectFromActiveJson.policyName,
                            activeType,
                            json.objectFromActiveJson.policyId,
                            null,
                            false));
                }
                break;
            case CustomerConstant.InteractionType.PROPOSAL_SEND:
                textView.append("发送计划书");
                if (json != null && json.objectFromActiveJson != null && json.objectFromActiveJson.proposalName != null) {
                    textView.append(getActionStrForNet(context,
                            json.objectFromActiveJson.proposalName,
                            activeType,
                            json.objectFromActiveJson.proposalId,
                            json.objectFromActiveJson.proposalUrl,
                            false));
                }
                break;
            case CustomerConstant.InteractionType.PROPOSAL_READ:
                if (json != null && json.objectFromActiveJson != null && json.objectFromActiveJson.proposalName != null) {
                    textView.append(getActionStrForNet(context,
                            json.objectFromActiveJson.proposalName,
                            activeType,
                            json.objectFromActiveJson.proposalId,
                            json.objectFromActiveJson.proposalUrl,
                            false));
                }
                break;
            case CustomerConstant.InteractionType.NOTE_CREATE:

                break;
            case CustomerConstant.InteractionType.NOTE_EDIT:

                break;
            case CustomerConstant.InteractionType.PHONE_CALL:

                break;
        }
    }
}